import * as vscode from 'vscode';
import { TodoItem } from './extension';

export class TodoTreeDataProvider implements vscode.TreeDataProvider<TodoItem> {
    private _onDidChangeTreeData: vscode.EventEmitter<TodoItem | undefined | null | void> = new vscode.EventEmitter<TodoItem | undefined | null | void>();
    readonly onDidChangeTreeData: vscode.Event<TodoItem | undefined | null | void> = this._onDidChangeTreeData.event;

    constructor(private todoStore: vscode.Memento) {}

    refresh(): void {
        this._onDidChangeTreeData.fire();
    }

    getTreeItem(element: TodoItem): vscode.TreeItem {
        return {
            label: element.label,
            collapsibleState: vscode.TreeItemCollapsibleState.None,
            contextValue: 'todoItem',
            command: {
                command: 'todo-manager.toggleTodo',
                title: 'Toggle Complete',
                arguments: [element]
            },
            iconPath: element.completed ? 
                new vscode.ThemeIcon('check') : 
                new vscode.ThemeIcon('circle-outline')
        };
    }

    getChildren(element?: TodoItem): Thenable<TodoItem[]> {
        // 如果没有元素（根节点），返回所有TODO
        if (!element) {
            return Promise.resolve(this.todoStore.get<TodoItem[]>('todos', []));
        }
        
        // TODO项没有子节点
        return Promise.resolve([]);
    }
}
    